package com.example.demo.controller;

import com.example.demo.service.LikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/likes")
public class LikeController {

    @Autowired
    private LikeService likeService;

    @PostMapping("/posts/{postId}/users/{userId}")
    public ResponseEntity<Void> likePost(@PathVariable Long postId,
                                         @PathVariable Long userId) {
        likeService.likePost(userId, postId);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/posts/{postId}/users/{userId}")
    public ResponseEntity<Void> unlikePost(@PathVariable Long postId,
                                           @PathVariable Long userId) {
        likeService.unlikePost(userId, postId);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/comments/{commentId}/users/{userId}")
    public ResponseEntity<Void> likeComment(@PathVariable Long commentId,
                                            @PathVariable Long userId) {
        likeService.likeComment(userId, commentId);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/comments/{commentId}/users/{userId}")
    public ResponseEntity<Void> unlikeComment(@PathVariable Long commentId,
                                              @PathVariable Long userId) {
        likeService.unlikeComment(userId, commentId);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/posts/{postId}/users/{userId}/status")
    public ResponseEntity<Boolean> isPostLikedByUser(@PathVariable Long postId,
                                                     @PathVariable Long userId) {
        return ResponseEntity.ok(likeService.isPostLikedByUser(userId, postId));
    }

    @GetMapping("/comments/{commentId}/users/{userId}/status")
    public ResponseEntity<Boolean> isCommentLikedByUser(@PathVariable Long commentId,
                                                        @PathVariable Long userId) {
        return ResponseEntity.ok(likeService.isCommentLikedByUser(userId, commentId));
    }

    @GetMapping("/posts/{postId}/count")
    public ResponseEntity<Integer> getPostLikeCount(@PathVariable Long postId) {
        return ResponseEntity.ok(likeService.getPostLikeCount(postId));
    }

    @GetMapping("/comments/{commentId}/count")
    public ResponseEntity<Integer> getCommentLikeCount(@PathVariable Long commentId) {
        return ResponseEntity.ok(likeService.getCommentLikeCount(commentId));
    }
}